package com.mogu.blogs.controller;

import com.mogu.blogs.pojo.BlogTagSort;
import com.mogu.blogs.service.TagService;
import com.mogu.entity.TBlog;
import com.mogu.entity.TTag;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2020/6/12.
 */
@RestController
@RequestMapping(value = "/blogs")
public class TagController {
        @Resource
        TagService tagService;
//        首页展示的tag标签 按热度sort正序排名 并只展示前20个
    @RequestMapping(value = "/getHotTag",method = RequestMethod.GET)
    public List<TTag> getHotTag(){
        List<TTag> hotTag = tagService.getHotTag();
        return hotTag;
    }

//    每点击一次标签 根据标签uid sort+1 修改数据库
    @RequestMapping(value = "/updateSortById/{uid}",method = RequestMethod.GET)
    public String updateSortById(@PathVariable("uid") String uid){
        Boolean aBoolean = tagService.updateSortByUid(uid);

        if(aBoolean){
            return "success";
        }
        return "fail";

    }
//    遍历所有标签
@RequestMapping(value = "/findAllTag",method = RequestMethod.GET)
public List<TTag> findAllTag(){
    List<TTag> allTag = tagService.findAllTag();
    return allTag;
}
    /**
     * 查询所有博客 按时间排序
     */
    @RequestMapping(value = "/findBlog", method = RequestMethod.POST)
    public List<TBlog> findBlog(@RequestBody Map<String,Integer> map) {
        Integer currPage = map.get("currPage");
        Integer pageSize = map.get("pageSize");
//        System.out.println(currPage+pageSize);
        System.out.println(currPage);
        System.out.println(pageSize);
        return tagService.findBlog(currPage, pageSize);
    }
//查询博客 按点击排行排序 前5 个

    @RequestMapping(value = "/findByCount" , method = RequestMethod.GET)
    public  List<TBlog> findByCount(){
        List<TBlog> byCount = tagService.findByCount();
        return byCount;
    }
    @GetMapping("/findTagByBlogUid/{uid}")
    public List<TTag> findTagByBlogUid(@PathVariable String uid){
        return this.tagService.findTagByBlogUid(uid);
    }

    /**
     * 根据标签的uid查询博客并按时间的降序排列
     * @param uid
     * @return
     */
    @ApiOperation(value = "根据标签的uid查询博客并按时间的降序排列" , notes = "根据标签的uid查询博客并按时间的降序排列")
    @RequestMapping(value = "/findBlogByTagUid/{uid}" , method = RequestMethod.GET)
    public List<BlogTagSort> findBlogByTagUid(@PathVariable String uid) {
        return tagService.findBlogByTagUid(uid);
    }
//收藏排行前2个
    @RequestMapping(value = "/findBycollect" , method = RequestMethod.GET)
    public  List<TBlog> findBycollect(){

        List<TBlog> bycollect = tagService.findBycollect();
        return bycollect;
    }
}
